package jdbc;



import entity.User;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author Ashin0013
 * @Date 2020/1/4 19:25
 */
public class Function implements UserDao{
    /**
     * 登入的MySql语句
     */
    private static final String SQL_USER_LOGIN = "select * from users WHERE id=? AND password=?";

    private static final String SQL_USER_INSERT = "insert into users VALUES(?,?,?,null,null,null,null,null )";
    private static final String SQL_USER_UPDATE= "update users set age=?,gender=?,address=?,motto=?,telephone=?,name=? WHERE id=?";
  //  private static final String SQL_USER_UPDATEGENDER= "update users set gender=? WHERE id=?";
 //   private static final String SQL_USER_UPDATEADDRESS= "update users set address=? WHERE id=?";
  //  private static final String SQL_USER_UPDATEMOTTO= "update users set motto=? WHERE id=?";
  // private static final String SQL_USER_UPDATETELEPHONE= "update users set telephone=? WHERE id=?";

    private static final String SQL_USER_ISIDEXIST = "select * from users WHERE id = ?";
    private static final String SQL_USER_FINDDATA = "select * from chat_private WHERE fromId = ? AND toId = ? ";
    private static final String SQL_USER_INSERTDATA = "insert into chat_private VALUES(?,?,?,?)";
    private static final String SQL_USER_GETID = "select id from users";
    private static final String SQL_USER_INSERTGROUPDATA = "insert into chat_group VALUES(?,?,?)";
    private static final String SQL_USER_FINDGROUPDATA = "select * from chat_group WHERE id = ?";
    private static final String SQL_USER_FINDPASSWORD = "select password from users WHERE id = ?";



    @Override
    public int login(User user) {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        try {
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_LOGIN);
            pre.setString(1, user.getId());
            pre.setString(2, user.getPassword());
            re = pre.executeQuery();
            while (re.next()) {
                //找到用户
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(con, pre, re);
        }
        return -1;
    }



    @Override
    public boolean isIdExist(String id) {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        try {
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_ISIDEXIST );
            pre.setString(1, id);
            re = pre.executeQuery();
            while (re.next()) {
                //找到用户
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(con, pre, re);
        }
        return false;
    }



    @Override
    public boolean insert(User user) {
        Connection con = null;
        PreparedStatement pre = null;
        try{
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_INSERT);
            pre.setString(1,user.getId());
            pre.setString(2,user.getPassword());
            pre.setString(3,user.getName());
            int line = pre.executeUpdate();
            if (line > 0){
                return true;
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con, pre, null);
        }
        return false;
    }



    @Override
    public void update(User user) {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        try{
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_UPDATE);
            pre.setString(1,user.getAge());
            pre.setString(2,user.getGender());
            pre.setString(3,user.getAddress());
            pre.setString(4,user.getMotto());
            pre.setString(5,user.getTelNum());
            pre.setString(6,user.getName());
            pre.setString(7,user.getId());
            int line = pre.executeUpdate();
            if(line > 0) {
                //找到用户
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con,pre,null);
        }
    }




   @Override
    public ArrayList<String> getPrivateChatRecord(String fromId, String toId) throws SQLException {
       ArrayList<String> all = new ArrayList<>();
       Connection con = null;
       PreparedStatement pre = null;
       ResultSet re = null;
       try{
           con = JdbcUtils.getConnection();
           pre = con.prepareStatement(SQL_USER_FINDDATA);
           pre.setString(1,fromId);
           pre.setString(2,toId);
           re = pre.executeQuery();
           String tmp = "";
           while (re.next()) {
               tmp = "[" + re.getString("time") +"]"+ "   ";
               tmp += "[" +re.getString("fromId") + "]" + "  to  ";
               tmp += "[" + re.getString("toId") +"]"+ "   ";
               tmp += re.getString("chatData");
               all.add(tmp);
           }

       }catch(SQLException e){
           e.printStackTrace();
       }finally {
           JdbcUtils.close(con,pre,null);
       }
       return all;
   }



    @Override
    public boolean insertPrivateChatData(String fromId, String toId, String data) {
        Connection con = null;
        PreparedStatement pre = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        String nowTime = dateFormat.format( now );
        try{
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_INSERTDATA);
            pre.setString(1,fromId);
            pre.setString(2,toId);
            pre.setString(3,nowTime);
            pre.setString(4,data);
            int line = pre.executeUpdate();
            if (line > 0){
                return true;
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con, pre, null);
        }
        return false;
    }



    @Override
    public User findUser(String id) {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        User user = null;
        try {
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_ISIDEXIST );
            pre.setString(1, id);
            re = pre.executeQuery();
            while (re.next()) {
                //找到用户
                String name = re.getString("name");
                String age = re.getString("age");
                String gender = re.getString("gender");
                String address = re.getString("address");
                String motto = re.getString("motto");
                String telephone = re.getString("telephone");
                user = new User(id, motto, name,age, gender,address, telephone,null);
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(con, pre, re);
        }
        return null;

    }



    @Override
    public ArrayList<String> getAllId() {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        User user = null;
        ArrayList<String> arr ;
        try {
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_GETID);
            re = pre.executeQuery();
            arr = new ArrayList<>();
            while (re.next()) {
                //找到用户
                String temp = re.getString("id");
                arr.add(temp);
            }
            return arr;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(con, pre, re);

        }
        return null;
    }



    @Override
    public boolean insertGroupChatRecord(String id, String message) {
        Connection con = null;
        PreparedStatement pre = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        String nowTime = dateFormat.format(now);
        try{
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_INSERTGROUPDATA);
            pre.setString(1,nowTime);
            pre.setString(2,message);
            pre.setString(3,id);
            int line = pre.executeUpdate();
            if (line > 0){
                return true;
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con, pre, null);
        }
        return false;
    }


    @Override
    public ArrayList<String> getGroupChatRecord(String id) throws SQLException {
        ArrayList<String> arrayList = new ArrayList<>();
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        try{
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_FINDGROUPDATA);
            pre.setString(1,id);
            re = pre.executeQuery();
            String tmp = "";
            while (re.next()) {
                tmp = "[" + re.getString("id") +"]"+ "   ";
                tmp += "[" + re.getString("time") +"]"+ "   ";
                tmp += re.getString("data");
                arrayList.add(tmp);
            }

        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con,pre,null);
        }
        return arrayList;
    }



    @Override
    public String getPassword(String id) {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        try{
            con = JdbcUtils.getConnection();
            pre = con.prepareStatement(SQL_USER_FINDPASSWORD);
            re = pre.executeQuery();
            String tmp = re.getString("password");
            return tmp;

        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con,pre,null);
        }
        return null;
    }
}




